package com.zhangye.test.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhangye.test.mapper.ProxyCompanyMapper;
import com.zhangye.test.model.Admin;
import com.zhangye.test.model.ProxyAr;
import com.zhangye.test.model.ProxyCompany;
import com.zhangye.test.util.HttpUtil;
import com.zhangye.test.util.PageInfo;
import com.zhangye.test.util.Result;

/**
 * 代理公司
 * 作者： 张叶
 * 创建时间：2018年9月20日  上午9:45:31
 */
@Service
public class ProxyCompanyService {

	@Autowired
	private ProxyCompanyMapper proxyCompanyMapper;
	@Autowired
	private AdminService adminService;
	@Autowired
	private ProxyArService proxyArService;
	
	
	
	@Transactional
	public Result auditTestProxyCompany(Long id){
		Result result = new Result();
		ProxyCompany pc = proxyCompanyMapper.selectByPrimaryKey(id);
		if(pc != null && !"".equals(pc)){
			String where = " and proxy_company_id = " + pc.getId();
			List<Admin> list = adminService.selectWhere(where);
			if(list.size() > 0){
				Admin admin = list.get(0);
				try{
					admin.setIsabled(0);
					admin.setRemark("test");
					adminService.updateAdmin(admin, 0);
					pc.setStatus("on");
					Admin admin1 = adminService.getCurrent();
					pc.setAuditAdminId(admin1.getId());
					pc.setAuditDate(new Date());
					String url = "https://arys.zhyell.com?id=" + pc.getId();
					pc.setLink(url);
					String urls = "https://arzf.zhyell.com?id=" + pc.getId();
					pc.setLinkSpare(urls);
					try{
						String shortUrl = HttpUtil.getShortUrl(url);
						pc.setShortLink(shortUrl);
						
						String shortUrlSpare = HttpUtil.getShortUrl(urls);
						pc.setShortLinkSpare(shortUrlSpare);
					}catch(Exception e){
						e.printStackTrace();
					}
					proxyCompanyMapper.updateByPrimaryKey(pc);
					result.code = 0;
					result.message = "审核成功";
				}catch(Exception e){
					result.code = 1;
					result.message = "系统错误，审核失败";
				}
			}else{
				result.code = 3;
				result.message = "此记录无效";
			}
		}else{
			result.code = 2;
			result.message = "该信息或已删除，请刷新重试";
		}
		return result;
	}
	
	
	/**
	 * 审核
	 * 作者： 张叶
	 * 创建时间：2018年9月25日  下午3:57:18
	 */
	@Transactional
	public Result auditProxyCompany(Long id){
		Result result = new Result();
		ProxyCompany pc = proxyCompanyMapper.selectByPrimaryKey(id);
		if(pc != null && !"".equals(pc)){
			String where = " and proxy_company_id = " + pc.getId();
			List<Admin> list = adminService.selectWhere(where);
			if(list.size() > 0){
				Admin admin = list.get(0);
				try{
					admin.setIsabled(0);
					adminService.updateAdmin(admin, 0);
					pc.setStatus("on");
					Admin admin1 = adminService.getCurrent();
					pc.setAuditAdminId(admin1.getId());
					pc.setAuditDate(new Date());
					String url = "https://arys.zhyell.com?id=" + pc.getId();
					pc.setLink(url);
					String urls = "https://arzf.zhyell.com?id=" + pc.getId();
					pc.setLinkSpare(urls);
					try{
						String shortUrl = HttpUtil.getShortUrl(url);
						pc.setShortLink(shortUrl);
						
						String shortUrlSpare = HttpUtil.getShortUrl(urls);
						pc.setShortLinkSpare(shortUrlSpare);
					}catch(Exception e){
						e.printStackTrace();
					}
					proxyCompanyMapper.updateByPrimaryKey(pc);
					result.code = 0;
					result.message = "审核成功";
				}catch(Exception e){
					result.code = 1;
					result.message = "系统错误，审核失败";
				}
			}else{
				result.code = 3;
				result.message = "此记录无效";
			}
		}else{
			result.code = 2;
			result.message = "该信息或已删除，请刷新重试";
		}
		return result;
	}
	
	
	/**
	 * 添加
	 * 作者： 张叶
	 * 创建时间：2018年9月25日  下午3:57:18
	 */
	public ProxyCompany selectById(Long id){
		ProxyCompany pc = proxyCompanyMapper.selectByPrimaryKey(id);
		return pc;
	}

	
	/**
	 * 条件
	 * 作者： 张叶
	 * 创建时间：2018年9月19日  上午11:54:33
	 */
	public List<ProxyCompany> selectWhere(String where){
		List<ProxyCompany> list = proxyCompanyMapper.selectWhere(where);
		return list;
	}

	
	/**
	 * 代理代表
	 * 作者： 张叶
	 * 创建时间：2018年9月19日  上午11:49:30
	 */
	public List<Map<String,Object>> selectByPage(PageInfo pageInfo, String where, String order) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("myCount", pageInfo.getCount());
		param.put("pageSize", pageInfo.getPageSize());
		param.put("page", pageInfo.getPage());
		param.put("where", where);
		param.put("order", order);
		List<Map<String, Object>> m = proxyCompanyMapper.selectByPage(param);
		pageInfo.setCount(Integer.parseInt(param.get("myCount").toString()));
		return m;
	}
	
	
	/**
	 * 添加
	 * 添加admin proxyCompany
	 * 作者： 张叶
	 * 创建时间：2018年9月21日  上午9:36:09
	 */
	@Transactional
	public Result insert(ProxyCompany pc){
		Result result = new Result();
		String telphone = pc.getTelphone();
		Admin admin = adminService.loginAdmin(telphone);
		if(admin == null){
			pc.setStatus("audit");
			pc.setCreatedAt(new Date());
			int i = proxyCompanyMapper.insert(pc);
			admin = new Admin();
			admin.setTelphone(pc.getTelphone());
			admin.setUsername(pc.getTelphone());
			admin.setRealname(pc.getName());
			admin.setPassword(DigestUtils.md5Hex(pc.getPassword()));
			admin.setCreatedate(new Date());
			admin.setCounts(2147483647);
			admin.setRoleid(6);
			admin.setProxyCompanyId(pc.getId());
			admin.setProxyId(pc.getProxyId());
			admin.setIsabled(1);
			admin.setMoney((double) 300);
			i += adminService.insertAdminAndAur(admin);// 保存会员信息
			if(i > 0){
				result.code = 0;
				result.message = "添加成功";
			}else{
				result.code = 1;
				result.message = "系统错误，添加失败";
			}
		}else{
			result.code = 3;
			result.message = "该手机号已存在，添加失败";
		}
		return result;
	}
	
	
	/**
	 * 删除代理公司，以及管理员账号
	 * 禁用发表作品
	 * 作者： 张叶
	 * 创建时间：2018年10月11日  上午9:51:20
	 */
	@Transactional
	public int deleted(ProxyCompany pc){
		int i = 0;
		try{
			pc.setStatus("deleted");
			i += proxyCompanyMapper.updateByPrimaryKey(pc);
			Admin admin = adminService.findByTelphone(pc.getTelphone());
			if(admin != null){
				List<ProxyAr> lists = proxyArService.selectByWhere(" and admin_id = " + admin.getId());
				for(ProxyAr pa:lists){
					i += proxyArService.deleted(pa.getId());
				}
				i += adminService.delAdminAndAur(admin.getId());
			}
		}catch(Exception e){
			
		}
		return i;
	}
	
	public int update(ProxyCompany pc){
		int i = proxyCompanyMapper.updateByPrimaryKey(pc);
		return i;
	}
}
